# -*- coding: utf-8 -*-
"""
Created on Fri Mar 26 11:16:29 2021

@author: Alex XIAO
"""

import pandas as pd
import numpy as np
from pandas import concat, to_datetime, date_range, merge
from numpy import nan, isinf
from pandas import DataFrame
import datetime
from math import isnan

from dateutil.relativedelta import relativedelta
import pickle
import sys
import matplotlib.pyplot as plt




def lag1(date):
    return pd.date_range(end= date,periods=2, freq="M")[0]


def lag12(date):
    return pd.date_range(end= date,periods=13, freq="M")[0]

def lag11(date):
    return pd.date_range(end= date,periods=13, freq="M")[1]

def lag24(date):
    return pd.date_range(end= date,periods=25, freq="M")[0]

def lag23(date):
    return pd.date_range(end= date,periods=25, freq="M")[1]

if __name__ == "__main__":
    
    y=pd.read_excel('../input/y集合.xlsx', index_col=0)

    emptydata=DataFrame(data = None,columns=y.columns, index = y.index)
    for i in range(25,len(y.index)):
        for j in range(len(y.columns)): 
            date1=y.index[i]
            growth1= y.loc[lag11(date1),:][j]-y.loc[lag12(date1),:][j] # 上年的下个月相对于这个月的差值
            growth2= y.loc[lag23(date1),:][j]-y.loc[lag24(date1),:][j] # 前年的下个月相对于这个月的差值
            sorted_y=list(y.iloc[i-12:i,j]) # 过去十二个月的数值
            Cmax =1.3 * (0.5*sorted_y[-1]+0.3*sorted_y[-2]+0.2*sorted_y[-3]) #计算一个综合值
            if y.loc[lag11(date1),:][j]<0: # 如果上年的下个月是负值
                # 那么本月值就是基准值和环比
                emptydata.iloc[i,j]=min(Cmax,(1+y.loc[lag1(date1),:][j]+growth2)/(1+y.loc[lag11(date1),:][j]))
            else:
                emptydata.iloc[i,j]= min(Cmax,y.loc[lag1(date1),:][j]+0.95*growth1)
    
    newy=y.loc["2018-01-01":,]
    season_y=emptydata.loc["2018-01-01":,:]
    for i in range(len(newy.columns)):
        plt.title(season_y.columns[i])
        plt.plot(season_y.iloc[:,i])
        plt.plot(newy.iloc[:,i])
        plt.legend(["Pred","Real"])
        plt.show()
        
            
